/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.*;
import cc.altius.service.GeoZoneService;
import cc.altius.service.HospitalService;
import cc.altius.service.SpecialistService;
import cc.altius.service.ZoneService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author santosh
 */
@Controller
public class SpecialistController {

    @Autowired
    private SpecialistService specialistService;
    @Autowired
    private ZoneService zoneService;
    @Autowired
    private GeoZoneService geoZoneService;
    @Autowired
    private HospitalService hospitalService;

    @ModelAttribute("geoZoneList")
    public List<GeographicalZone> populateGeoZoneList() {
        return this.geoZoneService.getGeoZoneList();
    }

//    @ModelAttribute("specialistList")
//    public List<Specialist> populatespecialistList() {
//        return this.specialistService.getspecialistList();
//    }
    @ModelAttribute("specialistTypeList")
    public List<SpecialistType> populatespecialistTypeList() {
        return this.specialistService.getspecialistTypeList();
    }

    @ModelAttribute("hospitalList")
    public List<Hospital> populateHospitalList() {
        return this.specialistService.getHospitalList();
    }

    @RequestMapping(value = "/specialist/addSpecialist.htm", method = RequestMethod.GET)
    public String showAddSpecialist(ModelMap modelMap) {
        Specialist specialist = new Specialist();
        modelMap.addAttribute("specialist", specialist);
        return "specialist/addSpecialist";
    }

    @RequestMapping(value = "/specialist/addSpecialist.htm", method = RequestMethod.POST)
    public String showAddSpecialist(@ModelAttribute("specialist") Specialist specialist) {
        this.specialistService.addSpecialist(specialist);
        return "redirect:../specialist/addSpecialist.htm?msg=new specialist added successfully..";
    }

    @RequestMapping(value = "/specialist/listSpecialist.htm", method = RequestMethod.GET)
    public String showSpecialistList(ModelMap modelMap) {
        List<Zone> zoneList = this.zoneService.getZoneList();
        List<Map<String, Object>> filterSpecialist = this.specialistService.getFilterSpecialist(0,0,0);
        modelMap.addAttribute("filterSpecialist", filterSpecialist);
        modelMap.addAttribute("specialistTypeId", 0);
        modelMap.addAttribute("zoneList",zoneList);
        modelMap.addAttribute("zoneId", 0);
        modelMap.addAttribute("hospitalId", 0);
        return "specialist/listSpecialist";
    }

    @RequestMapping(value = "/specialist/listSpecialist.htm", method = RequestMethod.POST)
    public String filterOnSpecialist(@RequestParam(value = "specialistTypeId", required = true, defaultValue = "0") int specialistTypeId,
            @RequestParam(value = "zoneId", required = true, defaultValue = "0") int zoneId,
            ModelMap modelMap,HttpServletRequest request) throws ServletRequestBindingException {
        
        String hospitalName = ServletRequestUtils.getStringParameter(request, "hospitalId");
        hospitalName.trim();
        int hospitalId = 0;
        
        if (!"".equals(hospitalName)) {
            String temp1[] = hospitalName.split("~");
            hospitalId = Integer.parseInt(temp1[1].trim());
        }
        System.out.println("hpospitalId :"+hospitalId);
        List<Zone> zoneList = this.zoneService.getZoneList();
        List<Map<String, Object>> filterSpecialist = this.specialistService.getFilterSpecialist(specialistTypeId,zoneId,hospitalId);
        modelMap.addAttribute("filterSpecialist", filterSpecialist);
        modelMap.addAttribute("zoneList", zoneList);
        modelMap.addAttribute("specialistTypeId", specialistTypeId);
        modelMap.addAttribute("zoneId", zoneId);
        modelMap.addAttribute("hospitalId", hospitalId);
        modelMap.addAttribute("hospitalName", hospitalName);
        return "specialist/listSpecialist";
    }
    
    @RequestMapping(value = "ajaxCallForHospitalListForSpecialist.htm", method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxHospitalPopulate(HttpServletRequest request) {//ajaxCallForHospitalList
        
        String str = request.getParameter("term");
        String json;
        List<Hospital> hospitalList = this.hospitalService.hospitalListForAutoComplete(str);
        Gson gson = new Gson();
        List list = new ArrayList();
        for (Hospital h : hospitalList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", h.getHospitalId());
            map.put("label", h.getHospitalName() + " ~ " + h.getHospitalId());
            map.put("value", h.getHospitalName() + " ~ " + h.getHospitalId());
            list.add(map);
        }
        Type typeList = new TypeToken<List>() {
        }.getType();
        json = gson.toJson(list, typeList);
        return json;
    }

    @RequestMapping(value = "/specialist/editSpecialist.htm", method = RequestMethod.GET)
    public String showEditSpecialist(@RequestParam("specialistId") String specialistId, ModelMap modelMap) {
        Specialist specialist = this.specialistService.getSpecialistBySpecialistId(Integer.parseInt(specialistId));
        modelMap.addAttribute("specialist", specialist);
        return "specialist/editSpecialist";
    }

    @RequestMapping(value = "/specialist/editSpecialist.htm", method = RequestMethod.POST)
    public String updateSpecialist(@ModelAttribute("specialist") Specialist specialist) {
        int specialistId = this.specialistService.updateSpecialist(specialist);

        if (specialistId != 0) {
            return "redirect:../specialist/listSpecialist.htm?msg=Update specialist profile successfully..";
        } else {
            return "redirect:../specialist/editSpecialist.htm?msg=Specialist update failed";
        }
    }

    @RequestMapping(value = "/appspecialist/addAppSpecialist.htm", method = RequestMethod.GET)
    public String showGetPage(@ModelAttribute("appSpecialist") AppSpecialist appSpecialist) {
//        AppSpecialist appSpecialist = new AppSpecialist();
//        modelMap.addAttribute("appSpecialist", appSpecialist);
        return "appspecialist/addAppSpecialist";
    }
//    public String showGetPage(ModelMap modelMap) {
//        AppSpecialist appSpecialist = new AppSpecialist();
//        modelMap.addAttribute("appSpecialist", appSpecialist);
//        return "appspecialist/addAppSpecialist";
//    }

    @RequestMapping(value = "/appspecialist/addAppSpecialist.htm", method = RequestMethod.POST)
    public String showPostPage(@ModelAttribute("appSpecialist") AppSpecialist appSpecialist, SessionStatus sessionStatus) {
        int appSpecialistId = this.specialistService.addAppSpecialist(appSpecialist);
        if (appSpecialistId == 0) {
            return "appspecialist/addAppSpecialist";
        } else {
            sessionStatus.setComplete();
            return "redirect:addAppSpecialist.htm?msg=Succesfully Added New Exclusive Specialist ";
        }
        //return "/appspecialist/listAppSpecialist";
    }

    @RequestMapping(value = "/appspecialist/listAppSpecialist.htm", method = RequestMethod.GET)
    public String showAppSpecialistList(ModelMap modelMap) {
//        List<AppSpecialist> appSpecialistList=this.specialistService.getAppSpecialistList();
//        modelMap.addAttribute("appSpecialistList", appSpecialistList);

        List<Map<String, Object>> filterAppSpecialist = this.specialistService.getFilterAppSpecialist(0);
        modelMap.addAttribute("filterAppSpecialist", filterAppSpecialist);
        modelMap.addAttribute("specialistTypeId", 0);
        return "appspecialist/listAppSpecialist";
    }

    @RequestMapping(value = "/appspecialist/listAppSpecialist.htm", method = RequestMethod.POST)
    public String filterOnAppSpecialist(@RequestParam(value = "specialistTypeId", required = true, defaultValue = "0") int specialistTypeId,
            ModelMap modelMap) {

        List<Map<String, Object>> filterAppSpecialist = this.specialistService.getFilterAppSpecialist(specialistTypeId);
        modelMap.addAttribute("filterAppSpecialist", filterAppSpecialist);
        modelMap.addAttribute("specialistTypeId", specialistTypeId);

        return "appspecialist/listAppSpecialist";
    }

    @RequestMapping(value = "/appspecialist/editAppSpecialist.htm", method = RequestMethod.GET)
    public String showEditSpecialist(@RequestParam(value = "appSpecialistId", required = true, defaultValue = "0") int appSpecialistId, ModelMap modelMap) {
        AppSpecialist appSpecialist = this.specialistService.getAppSpecialistByAppSpecialistId(appSpecialistId);
        modelMap.addAttribute("appSpecialist", appSpecialist);
        return "/appspecialist/editAppSpecialist";
    }

    @RequestMapping(value = "/appspecialist/editAppSpecialist.htm", method = RequestMethod.POST)
    public String updateSpecialist(@ModelAttribute("appSpecialist") AppSpecialist appSpecialist) {
        int appSpecialistId = this.specialistService.updateAppSpecialist(appSpecialist);
        if (appSpecialistId != 0) {
            return "redirect:../appspecialist/listAppSpecialist.htm?msg=Update AppSpecialist profile successfully..";
        } else {
            return "redirect:../appspecialist/editAppSpecialist.htm?msg=AppSpecialist update failed";
        }
    }
}
